// ==UserScript==
// @name         云学堂自动看视频
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @updateURL    https://gitee.com/hry8383/test/blob/master/learn.js
// @downloadURL  https://gitee.com/hry8383/test/blob/master/learn.js
// @match        https://jtljia.yunxuetang.cn/kng/course/package*
// @grant        none
// ==/UserScript==
(function() {
    'use strict';
    // Your code here...
    function gotoNextCourse() {
        var yunxuetangList = localStorage.getItem("yunxuetang_list")
        if (!!yunxuetangList) {
            var list1 = JSON.parse(yunxuetangList)
            if (list1.length > 0) {
                var c = list1.shift()
                console.log("成功进入下一个课程", "还有" + list1.length + "个课程待学习")
                localStorage.setItem("yunxuetang_list", JSON.stringify(list1))
                window.open(c, '_self')
            }
        }
    }

    var body = document.getElementsByTagName("body")[0]
    var myDiv = document.createElement("div")
    myDiv.setAttribute("z-index", 99999)
    myDiv.setAttribute("style", "position: fixed; top: 0; right: 0;")
    myDiv.style.backgroundColor = "green"
    body.append(myDiv)
    var studyList = document.createElement("input")
    studyList.setAttribute("id", "myStudyList")
    studyList.style.width = '400px'
    myDiv.append("输入下一个要学习的课程第一章节地址：")
    myDiv.append(studyList)
    var btn = document.createElement("button")
    btn.append("确认")
    btn.addEventListener('click', function () {
        var sl = document.getElementById("myStudyList")
        var newUrl = sl.value
        if (newUrl && newUrl.startsWith('http')) {
            var yunxuetangList = localStorage.getItem("yunxuetang_list")
            if (!!yunxuetangList) {
                var list1 = JSON.parse(yunxuetangList)
                if (list1 instanceof Array) {
                    if (!list1.includes(newUrl)) {
                        list1.push(newUrl)
                        localStorage.setItem("yunxuetang_list", JSON.stringify(list1))
                    }
                } else {
                    localStorage.setItem("yunxuetang_list", [newUrl])
                }
            } else {
                localStorage.setItem("yunxuetang_list", [newUrl])
            }
        }
    })
    myDiv.append(btn)

    setInterval(function(){
        var restart = document.getElementById("reStartStudy");
        if (!!restart) {
            var event = new MouseEvent("click");
            restart.dispatchEvent(event);
            window.myMousedown=!0
            console.log("继续观看")
        }
        var scheduleText = document.getElementById("ScheduleText").innerText
        if (scheduleText == "100%") {
            console.log("本课程已经学习完成")
            var list = document.getElementById("lcKnowledgeChapterView_divLabelHtmlContents")
            var dl = list.getElementsByTagName('dl')[0]
            var dd =dl.getElementsByClassName("active select")[0]
            var next = dd.nextElementSibling
            if (!!next) {
                var nextEle = next.children[2].children[0]
                if (!!nextEle && nextEle.getAttribute("onclick").indexOf("/exam/exampreview") < 0) {
                    console.log("进入本课程下一章节")
                    nextEle.click()
                } else {
                    gotoNextCourse()
                }
            } else {
                gotoNextCourse()
            }
        }
        var startPlay = document.getElementsByClassName("mlplayer-start")
        if (!!startPlay && startPlay.length > 0) {
            startPlay[0].click()
        }
        var video = document.getElementById("vjs_video_3_html5_api");
        if(!!video && video.paused){
            console.log("开始观看")
            video.muted=true;
            video.play()
        }
    }, 5000)
})();